<?php

?>//<- Made it invalid... this class is fully deprecated

namespace system\core;

Abstract class attribute
{

	private $key;
	private $val;
	private $delimiter;
	private $attribute_case_transform = MB_CASE_LOWER; //MB_CASE_UPPER, FALSE

	function __construct($keyvaluepair, $delimiter=false)
	{
		$pair = explode("=", $keyvaluepair, 2);
		$pair[0] = trim($pair[0]);
		$pair[1] = trim(@$pair[1]);

		if( $delimiter == false && $pair[1] == false )
		{
			$pair[1] = $pair[0];
		}
		elseif( $delimiter == false && $pair[1] != false )
		{

		}
		elseif( $delimiter )
		{
			$pair[1] = trim($pair[1]);
			if( $pair[1][0] === $delimiter && $pair[1][strlen($pair[1]) - 1] === $delimiter )
			{
				$pair[1] = substr($pair[1], 1, -1);
			}
		}
		else
		{
			throw new Exception("Error parsing attribute");
		}

		$this->setKey($pair[0]);
		$this->setVal($pair[1]);
		$this->setDelimiter($delimiter);
//		$pair[0] = mb_convert_case($pair[0], $this->case_transform);
//		list($this->key, $this->val) = $pair;
//		$this->delimiter = $delimiter;
		return $this;
	}

	public function is($name, $matchesValue=null, $case=null)
	{
		if( !$case && isset($matchesValue) ) $matchesValue = $this->case_transform($matchesValue, $this->attribute_case_transform);
		$name = $this->case_transform($name, $this->attribute_case_transform);
		$key = $this->getKey();
		$val = $this->getVal($case);

		if( $key == $name )
		{
			if( !isset($matchesValue) )
			{
				return true;
			}
			else
			{
				if( $matchesValue == $val )
				{
					return true;
				}
			}
		}
	}

	public function getKey()
	{
		return $this->case_transform($this->key, MB_CASE_LOWER);
	}

	public function setKey($key)
	{
		$this->key = $this->case_transform($key, $this->attribute_case_transform);
	}

	public function getVal($case=false)
	{
		if( $case ) return $this->val;
		return $this->case_transform($this->val, $this->attribute_case_transform);
	}

	public function setVal($value)
	{
		$this->val = $value;
	}

	public function setDelimiter($delimiter)
	{
		$this->delimiter = $delimiter;
	}

	public function getDelimiter($delimiter)
	{
		return $this->delimiter;
	}

	private function case_transform($value, $toCase=false)
	{
		if( $toCase === false )
		{
			return $value;
		}

		if( function_exists("\mb_convert_case") )
		{
			$value = \mb_convert_case($value, $toCase);
		}
		else
		{
			switch ($toCase)
			{
				case MB_CASE_LOWER:
					$value = strtolower($value);
					break;
				case MB_CASE_UPPER:
					$value = strtoupper($value);
					break;
			}
		}
		return $value;
	}

	function evalByOperator($operator, $needle) //Modified from Tag...
	{
		$haystack = $this->getVal();

		$hs = "Haystack[{$haystack}]";
		$nl = "Needle[{$needle}]";
		$searchOperatorIndex["="] = "{$nl} Matches Exactly {$hs}"; //Needle is exactly equal to haystack
		$searchOperatorIndex["|="] = "{$hs} Contains Prefix {$nl}"; //Needle is prefix of haystack
		$searchOperatorIndex["*="] = "{$hs} Contains Selector {$nl}"; //Needle is somewhere in the haystack
		$searchOperatorIndex["~="] = "{$hs} Contains Word Selector {$nl}"; //Needle is a full word somewhere in the haystack
		$searchOperatorIndex["$="] = "{$hs} Ends With Selector {$nl}"; //Needle matches the end of the haystack
		$searchOperatorIndex["!="] = "{$nl} Not Equal To {$hs}"; //Needle is not equal to haystack
		$searchOperatorIndex["^="] = "{$hs} Starts With Selector {$nl}"; //Needle matches the beginning of the haystack

		$translateOperator["="] = "Matches Exactly";
		$translateOperator["|="] = "Contains Prefix";
		$translateOperator["*="] = "Contains Selector";
		$translateOperator["~="] = "Contains Word Selector";
		$translateOperator["$="] = "Ends With Selector";
		$translateOperator["!="] = "Not Equal To";
		$translateOperator["^="] = "Starts With Selector";

		if( !isset($searchOperatorIndex[$operator]) ) throw new Exception("Unknown compare operator [{$operator}]");
//		echo "Debug: ".$searchOperatorIndex[$operator]."\n";

		$haystackLen = strlen($haystack);
		$needleLen = strlen($needle);
		if( $needle ) $matchAtPos = strpos($haystack, $needle);

		switch ($operator)
		{
			case "=":
				return ($haystack == $needle ? true : false);
				break;
			case "!=":
				return ($haystack != $needle ? true : false);
				break;
			case "*=":
				return ($matchAtPos !== false ? true : false);
				break;
			case "|=":
				$condition1 = ($matchAtPos === 0);
				$condition2 = (substr($haystack, $needleLen, 1) === "-");
				return (($condition1 && $condition2) === true);
				break;
			case "~=":
				$condition1 = ($needleLen > 0);
				$pieces = explode(" ", $haystack);
				$condition2 = in_array($needle, $pieces);
				return (($condition1 && $condition2) === true);
				break;
			case "^=":
				$condition1 = ($matchAtPos === 0);
				return ($condition1 === true ? true : false);
				break;
			case "$=":
				$condition1 = (($matchAtPos + $needleLen) == $haystackLen);
				return ($condition1 === true ? true : false);
				break;
			default:
				return false;
		}
	}

}

?>
